package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {
        //1. 设置分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        //2. 调用mapper接口查询
        List<User> userList = userMapper.selectList(userDto);

        //3. 解析并封装结果
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }


    /**
     * 添加用户信息
     *
     * @param user 用户对象，包含用户的基本信息
     */
    @Override
    public void addUser(User user) {
        // 设置用户更新时间和创建时间
        user.setUpdateTime(LocalDateTime.now());
        user.setCreateTime(LocalDateTime.now());

        // 生成默认密码并进行MD5加密
        String password = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        // 调用数据访问层保存用户信息
        userMapper.addUser(user);
    }


    @Override
    public void deleteBYId(List<Integer> ids) {
        userMapper.deleteBYId(ids);
    }

    @Override
    public User getById(Integer id) {
        User user = userMapper.getById(id);
        return user;
    }

    @Override
    public void upDate(User user) {
        user.setUpdateTime(LocalDateTime.now());
        // 生成默认密码并进行MD5加密
        String password = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.upDate(user);
    }

    @Override
    public List<User> listByRole(String roleLabel) {
        List<User> list = userMapper.listByRole(roleLabel);
        return list;
    }

    @Override
    public LoginResultVo login(User user) {
        User e = userMapper.login(user.getUsername());
        if (e == null) {
            throw new BusinessException("用户名或密码错误");
        }
        String s = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!s.equals(e.getPassword())) {
            throw new BusinessException("用户名或密码错误");
        }
        if (e.getStatus() != 1) {
            throw new BusinessException("用户被禁用");
        }
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(e.getId());
        loginResultVo.setUsername(e.getUsername());
        loginResultVo.setName(e.getName());
        loginResultVo.setImage(e.getImage());
        loginResultVo.setRoleLabel(e.getRoleLabel());
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", e.getId());
        claims.put("username", e.getUsername());
        String s1 = JwtUtils.generateToken(claims);
        loginResultVo.setToken(s1);

        return loginResultVo;
    }
}
